// Win32 icon implementation
// Copyright 2006 (C) Ralph Thomas

#include <toolkit/win32/icon-win32.h>
#include <toolkit/win32/util-win32.h>
#include <tchar.h>
#include <iostream>
#include <sstream>

namespace bfs = boost::filesystem;

namespace toolkit {
	iconWin32::~iconWin32() {}
	icon::~icon() {}
	
	class iconWin32Impl : public iconWin32 {
		HICON _small;
		HICON _regular;
		HICON _large;
	  public:
		iconWin32Impl( const bfs::path& file ) : _small( 0 ), _regular( 0 ), _large( 0 ) {
			std::string path( file.native_file_string() );
			//
			// Use SHGetFileInfoW to get out all of the icons
			// that we can.
			//
			SHFILEINFO fileInfo;
			if( SHGetFileInfo( path.c_str(), 0, &fileInfo, sizeof( fileInfo ), SHGFI_ICON | SHGFI_SMALLICON ) )
				_small = fileInfo.hIcon;
			if( SHGetFileInfo( path.c_str(), 0, &fileInfo, sizeof( fileInfo ), SHGFI_ICON ) )
				_regular = fileInfo.hIcon;
			if( SHGetFileInfo( path.c_str(), 0, &fileInfo, sizeof( fileInfo ), SHGFI_ICON | SHGFI_LARGEICON ) )
				_large = fileInfo.hIcon;
			//
			// XXX: Error checking here!
			//
		}
		virtual ~iconWin32Impl() {
			if( _small )	DestroyIcon( _small );
			if( _regular )	DestroyIcon( _regular );
			if( _large )	DestroyIcon( _large );
		}
		virtual HICON get( iconWin32::iconSize size ) const {
			if( size == kSmall )	return _small;
			if( size == kRegular )	return _regular;
			return _large;
		}
	};
	
	icon* icon::create( const bfs::path& path, const std::string& type ) {
		return new iconWin32Impl( path );
	}
	icon* icon::preview( const bfs::path& path ) {
		return 0;
	}
};
